package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pojo.pro.WorkflowProcess;
import com.qidianit.mes.pojo.pro.WorkflowProduct;
import com.qidianit.mes.pro.mapper.DictDataMapper;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.mapper.WorkflowProcessMapper;
import com.qidianit.mes.pro.mapper.WorkflowProductMapper;
import com.qidianit.mes.pro.service.WorkflowProcessService;
import com.qidianit.mes.pro.service.WorkflowProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: wang fei long
 * @Date: 2025/9/18 0018 - 19:29
 */
@Service
public class WorkflowProductServiceImpl implements WorkflowProductService {
    @Autowired
    private WorkflowProductMapper workflowProductMapper;


    @Override
    public Result<PageInfo<WorkflowProduct>> selectPage(Long workflowId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<WorkflowProduct> list = workflowProductMapper.selectPage(workflowId);
        return Result.success(new PageInfo<>(list));
    }

    @Override
    public Result<WorkflowProduct> selectById(Long id) {
        WorkflowProduct workflowProduct = workflowProductMapper.selectById(id);
        return workflowProduct != null ?
                Result.success(workflowProduct) :
                Result.error("工艺关联产品不存在");
    }

    @Override
    public Result add(WorkflowProduct workflowProduct) {
        // 检查是否已添加
        int count = workflowProductMapper.countByWorkflowAndProduct(
                workflowProduct.getWorkflowId(),
                workflowProduct.getProductCode()
        );
        if (count > 0) {
            return Result.error("该产品/物料已经添加");
        }

        return workflowProductMapper.insert(workflowProduct) > 0 ?
                Result.success() :
                Result.error("操作失败");
    }
    @Override
    public Result update(WorkflowProduct workflowProduct) {
        // 检查是否已添加（排除当前记录）
        WorkflowProduct old = workflowProductMapper.selectById(workflowProduct.getId());
        if (!old.getProductCode().equals(workflowProduct.getProductCode())) {
            int count = workflowProductMapper.countByWorkflowAndProduct(
                    workflowProduct.getWorkflowId(),
                    workflowProduct.getProductCode()
            );
            if (count > 0) {
                return Result.error("该产品/物料已经添加");
            }
        }

        return workflowProductMapper.updateById(workflowProduct) > 0 ?
                Result.success() :
                Result.error("操作失败");
    }


    @Override
    public Result delete(Long id) {
        return workflowProductMapper.deleteById(id)==1?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] id) {

        return workflowProductMapper.batchDelete(id)>0?Result.success():Result.error("操作失败");
    }


}